package com.jair.crawk.main;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WebClient webClient =  new WebClient(BrowserVersion.CHROME,"127.0.0.1",8888);//创建WebClient
		webClient.getOptions().setUseInsecureSSL(true);
		webClient.getOptions().setThrowExceptionOnScriptError(false);
		//webClient.setJavaScriptTimeout(10041);
		webClient.getOptions().setJavaScriptEnabled(false);
		webClient.getOptions().setCssEnabled(false);
		//webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		//webClient.getOptions().setTimeout(20000);
		try {
			String url = "https://cas.mioffice.cn/login?service=http%3A%2F%2Fopm.be.mi.com%2Fsite%2Flogin";
			String url2 = "https://cas.mioffice.cn/loginn";
			WebRequest request = new WebRequest(new URL(
					url2));
			HtmlPage page = webClient.getPage(request);
			//System.out.println(page);
			//HtmlPage page = webClient.getPage("https://cas.mioffice.cn/login");
			
//			HtmlElement switcher = (HtmlElement) page.getElementById("switcher_alogin");
			
//			HtmlElement loginBtn = (HtmlElement) page.getElementById("message_LOGIN_IMMEDIATELY");
//			switcher.focus();
//			switcher.click();
//			userName.focus();
//			userName.type("wenjie");
//			pwd.focus();
//			pwd.type("1026wjpyzhxWJ");
//			HtmlPage ret = loginBtn.click();
//			String result = page.asXml();//获得click()后的html页面（包括标签）
//			System.out.println(result);
			//final HtmlForm form = page.getFormByName(name)
		
//			 userName = (HtmlElement) page.getElementById("miniLogin_username");
//			 pwd = (HtmlElement) page.getElementById("miniLogin_pwd");
//			 loginBtn = (HtmlElement) page.getElementById("message_LOGIN_IMMEDIATELY");
//			userName.focus();
//			userName.type("wenjie");
//			pwd.focus();
//			pwd.type("1026wjpyzhxWJ");
//			 ret = loginBtn.click();
			//HtmlElement switcher = (HtmlElement) page.getElementById("switcher_alogin");
			//switcher.click();
	        //<input class="enter-item last-enter-item" id="miniLogin_username" autocomplete="off" type="text" placeholder="用户名或邮箱地址" name="username">
			HtmlInput  userName = page.getElementByName("username");
			//<input class="enter-item last-enter-item" title="邮箱密码" placeholder="邮箱密码" id="miniLogin_pwd" autocomplete="off" data-rule="" type="password" 
			HtmlInput  pwd = page.getElementByName("password");
			userName.focus();
			userName.type("wenjie");
			userName.setValueAttribute("wenjie");
			pwd.focus();
			pwd.type("1026wjpyzhxWJ");
			pwd.setValueAttribute("1026wjpyzhxWJ");
			//System.out.println( page.getElementById("message_LOGIN_IMMEDIATELY"));
			HtmlElement   button = (HtmlElement) page.getElementById("message_LOGIN_IMMEDIATELY");
			//button.click();
			////button.click();
			button.click();
	        // Now submit the form by clicking the button and get back the second page.
	         HtmlPage page2 = button.click();
	         
	         //List newLinks = (List) page.getByXPath ("//input[@class='loginBtn']/a");
	           // HtmlAnchor newlink = (HtmlAnchor)newLinks.get(0);
	           // newlink.click();
			 String result = page2.asXml();//获得click()后的html页面（包括标签）
			System.out.println("============");
			System.out.println(result);
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
